package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.widget.LinearLayout
import androidx.core.widget.addTextChangedListener
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityEditDescribeInfoBinding
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.finish

/**
 * @Author      : hxw
 * @Date        : 2024/8/24 10:55
 * @Describe    : 编辑描述信息
 */
class ActivityEditDescribeInfo : BaseActivity<BreedingPigeonViewModel, ActivityEditDescribeInfoBinding>() {

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        val title = intent.getStringExtra("title")
        mDatabind.includeBar.tvTitle.text = title
        val describe = intent.getStringExtra("describe").toString()
        mDatabind.etDescribeInfo.setText(describe)
        mDatabind.etDescribeInfo.setSelection(describe.length)

        mDatabind.etDescribeInfo.addTextChangedListener {
            if (it != null) {
                mDatabind.tvMaxNumber.text = it.length.toString() + "/2000"
            }
        }
        val type = intent.getIntExtra("type", 0)
        if (type == 1) {
            mDatabind.etDescribeInfo.hint = "请输入您爱鸽的体型、性格、特点、经历等内容......"
        } else {
            mDatabind.etDescribeInfo.hint = "请输入您爱鸽的比赛名、比赛名次、获奖情况等赛绩内容......"
        }

        /** 解决软键盘弹出按钮上移 */
        mDatabind.btnSaveEditDescribeInfo.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            mDatabind.btnSaveEditDescribeInfo.getWindowVisibleDisplayFrame(r)

            val screenHeight = mDatabind.btnSaveEditDescribeInfo.rootView.height
            val keyboardHeight = screenHeight - r.bottom

            // 根据键盘的状态调整布局的大小
            if (keyboardHeight > screenHeight * 0.15) {
                // 键盘弹出
                val layoutParams = mDatabind.btnSaveEditDescribeInfo.layoutParams as LinearLayout.LayoutParams
                layoutParams.setMargins(40, 0, 40, keyboardHeight)
                mDatabind.btnSaveEditDescribeInfo.layoutParams = layoutParams
            } else {
                // 键盘收起
                val layoutParams = mDatabind.btnSaveEditDescribeInfo.layoutParams as LinearLayout.LayoutParams
                layoutParams.setMargins(40, 0, 40, 70)
                mDatabind.btnSaveEditDescribeInfo.layoutParams = layoutParams
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.btnSaveEditDescribeInfo) {
            when (it) {
                mDatabind.btnSaveEditDescribeInfo -> {
                    val intent = Intent()
                    intent.putExtra("describe", mDatabind.etDescribeInfo.text.toString().trim())
                    finish(intent)
                }

                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}